package com.wula.gtsns.dao;

import com.wula.gtsns.entity.Zipcode;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * Zipcode entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wula.gtsns.entity.Zipcode
 * @author MyEclipse Persistence Tools
 */

public class ZipcodeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ZipcodeDAO.class);
	// property constants
	public static final String AREAID = "areaid";
	public static final String ZIP = "zip";
	public static final String CODE = "code";

	public void save(Zipcode transientInstance) {
		log.debug("saving Zipcode instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Zipcode persistentInstance) {
		log.debug("deleting Zipcode instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Zipcode findById(java.lang.Integer id) {
		log.debug("getting Zipcode instance with id: " + id);
		try {
			Zipcode instance = (Zipcode) getSession().get(
					"com.wula.gtsns.orm.Zipcode", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Zipcode> findByExample(Zipcode instance) {
		log.debug("finding Zipcode instance by example");
		try {
			List<Zipcode> results = (List<Zipcode>) getSession()
					.createCriteria("com.wula.gtsns.orm.Zipcode").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Zipcode instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Zipcode as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Zipcode> findByAreaid(Object areaid) {
		return findByProperty(AREAID, areaid);
	}

	public List<Zipcode> findByZip(Object zip) {
		return findByProperty(ZIP, zip);
	}

	public List<Zipcode> findByCode(Object code) {
		return findByProperty(CODE, code);
	}

	public List findAll() {
		log.debug("finding all Zipcode instances");
		try {
			String queryString = "from Zipcode";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Zipcode merge(Zipcode detachedInstance) {
		log.debug("merging Zipcode instance");
		try {
			Zipcode result = (Zipcode) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Zipcode instance) {
		log.debug("attaching dirty Zipcode instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Zipcode instance) {
		log.debug("attaching clean Zipcode instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}